<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/Typescript/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/Typescript/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/Typescript/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/Typescript/images/logo.svg" color="#222">

<link rel="stylesheet" href="/Typescript/css/main.css">


<link rel="stylesheet" href="/Typescript/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/Typescript/","scheme":"Muse","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="类型声明为什么要使用类型声明？？ 声明文件可以让我们不需要将JS重构为TS,  只需要加上声明文件即可  类型声明在编译的时候都会被删除掉，不会影响真正的代码  使用关键字declare(除了interface 和 type外)声明  常用的全局类型声明 注意的是，不能在该文件下使用export export default导出，否则这些声明就不是声明全局的了 123456declare var">
<meta property="og:type" content="article">
<meta property="og:title" content="Typescript&#x2F;类型声明">
<meta property="og:url" content="http://yoursite.com/2020/11/24/Typescript/%E7%B1%BB%E5%9E%8B%E5%A3%B0%E6%98%8E/index.html">
<meta property="og:site_name" content="Typescript">
<meta property="og:description" content="类型声明为什么要使用类型声明？？ 声明文件可以让我们不需要将JS重构为TS,  只需要加上声明文件即可  类型声明在编译的时候都会被删除掉，不会影响真正的代码  使用关键字declare(除了interface 和 type外)声明  常用的全局类型声明 注意的是，不能在该文件下使用export export default导出，否则这些声明就不是声明全局的了 123456declare var">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-11-24T04:18:25.203Z">
<meta property="article:modified_time" content="2020-11-24T08:01:22.466Z">
<meta property="article:author" content="HC">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/11/24/Typescript/%E7%B1%BB%E5%9E%8B%E5%A3%B0%E6%98%8E/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>Typescript/类型声明 | Typescript</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/Typescript/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Typescript</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/Typescript/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/Typescript/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="en">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/11/24/Typescript/%E7%B1%BB%E5%9E%8B%E5%A3%B0%E6%98%8E/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/Typescript/images/avatar.gif">
      <meta itemprop="name" content="HC">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Typescript">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Typescript/类型声明
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-11-24 12:18:25 / Modified: 16:01:22" itemprop="dateCreated datePublished" datetime="2020-11-24T12:18:25+08:00">2020-11-24</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="类型声明"><a href="#类型声明" class="headerlink" title="类型声明"></a>类型声明</h1><h2 id="为什么要使用类型声明？？"><a href="#为什么要使用类型声明？？" class="headerlink" title="为什么要使用类型声明？？"></a>为什么要使用类型声明？？</h2><ul>
<li><p>声明文件可以让我们不需要将JS重构为TS,  只需要加上声明文件即可</p>
</li>
<li><p>类型声明在编译的时候都会被删除掉，不会影响真正的代码</p>
</li>
<li><p>使用关键字<code>declare</code>(除了interface 和 type外)声明</p>
</li>
<li><p>常用的全局类型声明</p>
<p><strong>注意的是，不能在该文件下使用export export default导出，否则这些声明就不是声明全局的了</strong></p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">declare</span> <span class="keyword">var</span>  <span class="comment">// 声明全局变量</span></span><br><span class="line"><span class="keyword">declare</span> <span class="function"><span class="keyword">function</span>  // 声明全局方法</span></span><br><span class="line"><span class="function"><span class="title">declare</span> <span class="title">class</span> // 声明全局类</span></span><br><span class="line"><span class="function"><span class="title">declare</span> <span class="title">enum</span> // 声明全局枚举类型</span></span><br><span class="line"><span class="function"><span class="title">declare</span> <span class="title">namespace</span> // 声明全局对象</span></span><br><span class="line"><span class="function"><span class="title">interface</span> 和 <span class="title">type</span>  // 声明全局类型</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>普通类型声明</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">declare</span> <span class="keyword">let</span> myName: <span class="built_in">string</span>;</span><br><span class="line"><span class="keyword">declare</span> <span class="keyword">let</span> age: <span class="built_in">number</span>;</span><br><span class="line"><span class="keyword">declare</span> <span class="function"><span class="keyword">function</span> <span class="title">getInfo</span>(<span class="params">name: <span class="built_in">string</span>, age: <span class="built_in">number</span></span>): <span class="title">boolean</span></span>;</span><br><span class="line"><span class="keyword">declare</span> <span class="keyword">class</span> Person &#123; <span class="keyword">public</span> oName: <span class="built_in">string</span>; <span class="keyword">public</span> age: <span class="built_in">number</span> &#125;;</span><br><span class="line"><span class="keyword">declare</span> <span class="keyword">namespace</span> myNameSpace &#123;</span><br><span class="line">    <span class="keyword">const</span> name: <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">const</span> age: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> Interface &#123;</span><br><span class="line">    name: <span class="built_in">string</span>;</span><br><span class="line">    age: <span class="built_in">number</span>;</span><br><span class="line">    address: <span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">type</span> MyType = &#123; name: <span class="built_in">string</span>; age: <span class="built_in">number</span>; <span class="keyword">type</span>: <span class="built_in">string</span> &#125;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>声明jQuery对象</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// a.ts</span></span><br><span class="line"><span class="keyword">declare</span> <span class="keyword">const</span> $: <span class="function">(<span class="params">selector: <span class="built_in">string</span></span>) =&gt;</span> &#123;</span><br><span class="line">    on():<span class="built_in">void</span>;</span><br><span class="line">    width(length: <span class="built_in">number</span>): <span class="built_in">void</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// b.ts</span></span><br><span class="line"><span class="keyword">export</span> &#123;&#125;; <span class="comment">// 加上, 否则是重复定义</span></span><br><span class="line"><span class="comment">// 实现这个方法</span></span><br><span class="line"><span class="keyword">const</span> $ = <span class="function">(<span class="params">selector: <span class="built_in">string</span></span>) =&gt;</span> (&#123;</span><br><span class="line">    on() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'afdsafs'</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    width() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'fjds'</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">const</span> r = $(<span class="string">'a'</span>).on();</span><br><span class="line"><span class="built_in">console</span>.log(r);</span><br></pre></td></tr></table></figure>
</li>
<li><p>外部枚举</p>
<ul>
<li><p>使用关键字<code>declare enum</code>定义的枚举类型</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">declare</span> <span class="keyword">enum</span> Color &#123;</span><br><span class="line">    Red,</span><br><span class="line">    Yellow,</span><br><span class="line">    Blue,</span><br><span class="line">    Green,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> colors = [</span><br><span class="line">    Color.Blue,</span><br><span class="line">    Color.Green,</span><br><span class="line">    Color.Red,</span><br><span class="line">    Color.Yellow</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 编译的结果</span></span><br><span class="line"><span class="keyword">var</span> colors = [</span><br><span class="line">    Color.Blue,</span><br><span class="line">    Color.Green,</span><br><span class="line">    Color.Red,</span><br><span class="line">    Color.Yellow</span><br><span class="line">];</span><br></pre></td></tr></table></figure>
</li>
<li><p>常量枚举</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">declare</span> <span class="keyword">const</span> <span class="keyword">enum</span> Color &#123;</span><br><span class="line">    Red,</span><br><span class="line">    Yellow,</span><br><span class="line">    Blue,</span><br><span class="line">    Green,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> colors = [</span><br><span class="line">    Color.Blue,</span><br><span class="line">    Color.Green,</span><br><span class="line">    Color.Red,</span><br><span class="line">    Color.Yellow</span><br><span class="line">]</span><br><span class="line"><span class="comment">// 编译结果</span></span><br><span class="line"><span class="keyword">var</span> colors = [</span><br><span class="line">  <span class="number">0</span>, <span class="comment">/*Blue*/</span></span><br><span class="line">  <span class="number">1</span>, <span class="comment">/*Green*/</span></span><br><span class="line">  <span class="number">2</span>, <span class="comment">/*Red*/</span></span><br><span class="line">  <span class="number">3</span> <span class="comment">/*Yellow*/</span></span><br><span class="line">]</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>namespace</p>
<ul>
<li><p>如果一个全局变量包括了很多的子属性，那么可能会使用到namespace</p>
</li>
<li><p>在声明文件中的namespace表示一个全局变量包括很多的子属性</p>
</li>
<li><p>在命名空间内部不需要使用declare声明属性或方法</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">declare</span> <span class="keyword">namespace</span> Person &#123;</span><br><span class="line">    <span class="keyword">const</span> name: <span class="built_in">string</span>;</span><br><span class="line">    <span class="keyword">const</span> age: <span class="built_in">number</span>;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">eat</span>(<span class="params">food: <span class="built_in">string</span></span>):<span class="title">void</span></span>;</span><br><span class="line">    <span class="keyword">namespace</span> Children &#123;</span><br><span class="line">        <span class="keyword">const</span> address: <span class="built_in">string</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 使用的时候就有提示了</span></span><br><span class="line">Person.name;</span><br><span class="line">Person.age;</span><br><span class="line">Person.eat(<span class="string">'rice'</span>);</span><br><span class="line">Person.Children.address;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>类型声明文件</p>
<ul>
<li><p>我们可以把类型声明放在一个单独的类型声明文件中去</p>
</li>
<li><p>文件的命名规范为<code>xx.d.ts</code></p>
</li>
<li><p>好处是: 看类型声明文件有助于对库的使用方式</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  src/typings/jquery.d.ts</span></span><br><span class="line"><span class="keyword">declare</span> <span class="keyword">const</span> $: <span class="function">(<span class="params">selector: <span class="built_in">string</span></span>) =&gt;</span> &#123;</span><br><span class="line">    on(): <span class="built_in">void</span>;</span><br><span class="line">    width(length: <span class="built_in">number</span>): <span class="built_in">void</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// tsconfig.json  增加配置</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">"include"</span>: [</span><br><span class="line">    <span class="string">"src/**/*"</span> <span class="comment">// 表示的是找src下的所有目录下所有文件</span></span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//   src/test.ts</span></span><br><span class="line"><span class="comment">// 已经有提示了</span></span><br><span class="line">$(<span class="string">'#app'</span>).on();</span><br><span class="line">$(<span class="string">'#app'</span>).width(<span class="number">123</span>);</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>第三方声明文件</p>
<ul>
<li><p>如果有的库不是用ts写的，那么一般都有第三方的声明文件，在安装这个库时，如果没有内置声明文件，那么需要进行下载</p>
</li>
<li><p>@types是一个约定的前缀，所有的第三方声明的类型库都会带上这个前缀</p>
</li>
<li><p>Javascript的许多内置对象，Typescript官方已经给我们写好了，放在了全局作用域下，可以直接使用。</p>
</li>
</ul>
</li>
<li><p>安装jquery, 并且使用安装其声明文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">// 安装  </span><br><span class="line">cnpm install jquery -S;</span><br><span class="line">cnpm install @types/jquery;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>自己编写声明文件</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 根目录  typings/jquery/index.d.ts</span></span><br><span class="line"><span class="keyword">declare</span> <span class="function"><span class="keyword">function</span> <span class="title">jQuery</span>(<span class="params">selector:<span class="built_in">string</span></span>):<span class="title">HTMLElement</span></span>;</span><br><span class="line"><span class="keyword">declare</span> <span class="keyword">namespace</span> jQuery&#123;</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">ajax</span>(<span class="params">url:<span class="built_in">string</span></span>):<span class="title">void</span></span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"><span class="title">export</span> <span class="title">default</span> <span class="title">jQuery</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// tsconfig.json增加配置</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">"compilerOptions"</span>: &#123;</span><br><span class="line">    <span class="string">"target"</span>: <span class="string">"es5"</span>,</span><br><span class="line">    <span class="string">"module"</span>: <span class="string">"commonjs"</span>,</span><br><span class="line">    <span class="string">"strict"</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="string">"esModuleInterop"</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="string">"skipLibCheck"</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="string">"forceConsistentCasingInFileNames"</span>: <span class="literal">true</span>,</span><br><span class="line">  <span class="comment">// 增加baseUrl和paths字段    </span></span><br><span class="line">    <span class="string">"baseUrl"</span>: <span class="string">"./"</span>,</span><br><span class="line">    <span class="string">"paths"</span>: &#123;</span><br><span class="line">      <span class="string">"*"</span>: [</span><br><span class="line">        <span class="string">"typings/*"</span></span><br><span class="line">      ]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>npm 声明文件可能所在的位置</p>
<ul>
<li><p>node_modules/jquery/package.json  中的 types</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="attr">"types"</span>: <span class="string">"types/xxx.d.ts"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>node_modules/jquery/index.d.ts</p>
</li>
<li><p>node_modules/@types/jquery/index.d.ts</p>
</li>
<li><p>typings/jquery/index.d.ts</p>
</li>
</ul>
</li>
<li><p>查找声明文件</p>
<ol>
<li><p>给package.json增加一个<code>types</code>或<code>typings</code>字段指定一个声明文件的地址</p>
</li>
<li><p>在项目的根目录下，有一个index.d.ts</p>
</li>
<li><p>在package.json中的main字段指定的入口文件，有一个同名不同后缀的字段</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">"name"</span>: <span class="string">"myLib"</span>,</span><br><span class="line">    <span class="string">"version"</span>: <span class="string">"1.0.0"</span>,</span><br><span class="line">    <span class="string">"main"</span>: <span class="string">"lib/index.js"</span>,</span><br><span class="line">    <span class="string">"types"</span>: <span class="string">"myLib.d.ts"</span>,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<ul>
<li>查找方式<ol>
<li>先找myLib.d.ts</li>
<li>没有找 index.d.ts</li>
<li>如果没有再找 lib/index.d.ts</li>
<li>如果上面三个都没有，就认为没有声明文件了</li>
</ol>
</li>
</ul>
</li>
<li><p>扩展变量类型</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 扩展字符串的原型</span></span><br><span class="line"><span class="keyword">interface</span> String &#123;</span><br><span class="line">    eat():<span class="built_in">void</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> str = <span class="string">'hchlq'</span>;</span><br><span class="line">str.eat();</span><br></pre></td></tr></table></figure>
</li>
<li><p>模块内全局扩展</p>
<figure class="highlight plain"><figcaption><span>global &#123;&#125;```进行扩展</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&#96;&#96;&#96;ts</span><br><span class="line">declare global &#123;</span><br><span class="line">    interface String &#123;</span><br><span class="line">        study():void;</span><br><span class="line">    &#125;</span><br><span class="line">    interface Window &#123;</span><br><span class="line">        run():void;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">export &#123;&#125;; &#x2F;&#x2F; 必须要导出，再外部不能使用</span><br></pre></td></tr></table></figure>
</li>
<li><p>合并声明</p>
<ul>
<li>同一个名称的两个独立声明会被合并成一个单一声明</li>
<li>合并后的声明拥有原先两个声明的特性</li>
<li>一般有类型声明合并的类型:  函数，类，接口</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 函数</span></span><br><span class="line"><span class="comment">// 常见的是重载 </span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getName</span>(<span class="params">name: <span class="built_in">string</span></span>):<span class="title">string</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getName</span>(<span class="params">name: <span class="built_in">number</span></span>):<span class="title">number</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getName</span>(<span class="params">name: <span class="built_in">string</span> | <span class="built_in">number</span></span>): <span class="title">string</span> | <span class="title">number</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> name === <span class="string">'string'</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'hchlq'</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">123</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 或者命名空间扩展函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getName</span>(<span class="params">name: <span class="built_in">string</span></span>): <span class="title">string</span> </span>&#123;</span><br><span class="line">    getName.name;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">'hchlq'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">namespace</span> getName &#123;</span><br><span class="line">    <span class="keyword">export</span> <span class="keyword">let</span> name: <span class="built_in">string</span> = <span class="string">'string'</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 类</span></span><br><span class="line"><span class="keyword">class</span> Test &#123;</span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> Test &#123;</span><br><span class="line">    age: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> obj: Test = &#123;</span><br><span class="line">    name: <span class="string">'string'</span>,</span><br><span class="line">    age: <span class="number">18</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 命名空间扩展类</span></span><br><span class="line"><span class="keyword">class</span> Test &#123;</span><br><span class="line">    <span class="keyword">public</span> name: <span class="built_in">string</span> = <span class="string">'hchlq'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">namespace</span> Test &#123;</span><br><span class="line">    <span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">eat</span>(<span class="params"></span>):<span class="title">void</span> </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'person can eat'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 接口合并</span></span><br><span class="line"><span class="keyword">interface</span> Alarm &#123;</span><br><span class="line">    price: <span class="built_in">number</span>;</span><br><span class="line">    alert(s: <span class="built_in">string</span>): <span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> Alarm &#123;</span><br><span class="line">    weight: <span class="built_in">number</span>;</span><br><span class="line">    alert(s: <span class="built_in">string</span>, n: <span class="built_in">number</span>): <span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/Typescript/2020/11/23/Typescript/%E6%A8%A1%E5%9D%97%E5%92%8C%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4/" rel="prev" title="Typescript/模块和命名空间">
      <i class="fa fa-chevron-left"></i> Typescript/模块和命名空间
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#类型声明"><span class="nav-number">1.</span> <span class="nav-text">类型声明</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#为什么要使用类型声明？？"><span class="nav-number">1.1.</span> <span class="nav-text">为什么要使用类型声明？？</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">HC</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/Typescript/archives/">
        
          <span class="site-state-item-count">12</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HC</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> v4.2.1
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">Theme – <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/Typescript/lib/anime.min.js"></script>
  <script src="/Typescript/lib/velocity/velocity.min.js"></script>
  <script src="/Typescript/lib/velocity/velocity.ui.min.js"></script>

<script src="/Typescript/js/utils.js"></script>

<script src="/Typescript/js/motion.js"></script>


<script src="/Typescript/js/schemes/muse.js"></script>


<script src="/Typescript/js/next-boot.js"></script>




  















  

  

</body>
</html>
